
#include "SettingTask.h"

xtime time_set;
int selected;

__task void TaskBlink(void){
    char buffer[20];
    static int temp=0;
    PrintClockString(time_set);
    PrintDateString(time_set);
    os_itv_set(333);
    while (1){
        os_itv_wait();
        if (temp){
            switch (selected) {
                case 0:
                    sprintf(buffer,"*** ##:%02d:%02d ***",time_set.menit,time_set.detik);
                    break;
                case 1:
                    sprintf(buffer,"*** %02d:##:%02d ***",time_set.jam,time_set.detik);
                    break;
                case 2:
                    sprintf(buffer,"*** %02d:%02d:## ***",time_set.jam,time_set.menit);
                    break;
                case 3:
                    sprintf(buffer,"*** ## %s 20%d ",getMonName(time_set.bulan),time_set.tahun);
                    break;
                case 4:
                    sprintf(buffer,"*** %02d ### 20%02d ",time_set.tanggal,time_set.tahun);
                    break;
                case 5:              
                    sprintf(buffer,"*** %02d %s 20## ",time_set.tanggal,getMonName(time_set.bulan));
                    break;
            }
            if (selected<3){
                print_lcd(LCD_CLOCK_POS, buffer);    
            } else {
                print_lcd(LCD_DATE_POS, buffer);
            }
        } else {
            PrintClockString(time_set);
            PrintDateString(time_set);
        }        
        temp^=1; 
  }             
}

__task void TaskEditTime(void){
     // isi time_set dengan waktu sekarang
    int x,last=5;  
    int temp;
    OS_TID idBlinkTask;
    time_set = RTCGetTime();

    selected = 0;
    OpenKeyPad();   

    idBlinkTask = os_tsk_create(TaskBlink    ,50);

    while (1){
        print_lcd(0,"### Set Time ###");

        x = Scankey();  
        //penekanan tombol terus menerus tidak dihiraukan
        if (last) {last=x;x=0;}else{last=x;}  
           
        switch (x){
            case UP:
                switch (selected){
                    case 0: ++time_set.jam; break;
                    case 1: ++time_set.menit; break;
                    case 2: ++time_set.detik; break;
                    case 3: ++time_set.tanggal; break;
                    case 4: ++time_set.bulan; break;
                    case 5: ++time_set.tahun; break;
                }
                if(selected <3){
                    if (time_set.jam>23){time_set.jam=0;}
                    if (time_set.menit>59){time_set.menit=0;}
                    if (time_set.detik>59){time_set.detik=0;} 
                } else {
                    if (time_set.tahun > 99) {time_set.tahun = 0;}
                    if (time_set.bulan > 12) {time_set.bulan = 1;} 
                    temp = DayInMonth(time_set.bulan, time_set.tahun);
                    if (time_set.tanggal > temp) {time_set.tanggal= 1;}
                    if (time_set.tanggal < 1) {time_set.tanggal = temp;} 
                }                        
                break;
            case DOWN:
                switch (selected){
                    case 0: --time_set.jam; break;
                    case 1: --time_set.menit; break;
                    case 2: --time_set.detik; break;
                    case 3: --time_set.tanggal; break;
                    case 4: --time_set.bulan; break;
                    case 5: --time_set.tahun; break;
                }
                if(selected <3){
                    if (time_set.jam<0){time_set.jam=23;}
                    if (time_set.menit<0){ time_set.menit=59;}
                    if (time_set.detik<0){time_set.detik=59;}
                }else{
                    if (time_set.tahun  < 0) {time_set.tahun  = 99;}
                    if (time_set.bulan < 1) {time_set.bulan = 12;}  
                    temp = DayInMonth(time_set.bulan, time_set.tahun);
                    if (time_set.tanggal > temp) {time_set.tanggal= 1;}
                    if (time_set.tanggal < 1) {time_set.tanggal = temp;}  
                }      
                break;
            case LEFT:
                selected=(selected==0)?5:--selected;
                break;
            case RIGHT:
                selected=(selected==5)?0:++selected;
                break;
            case OK:
                //set jam ke register
                RTCSetTime(time_set);
                                                 
                print_lcd(0,"###  Sukses  ###");
                os_tsk_delete(idBlinkTask);
                os_dly_wait(700);

                clr_all_pannal(); 
                //time_set = RTCGetTime();
                PrintClockString(time_set);
                PrintDateString(time_set);   
                                           
                os_tsk_delete_self();
                break;
            case BACK:      
                print_lcd(0,"## Cancelled ###");
                os_tsk_delete(idBlinkTask);
                os_dly_wait(700);

                clr_all_pannal(); 
                time_set = RTCGetTime();
                PrintClockString(time_set);
                PrintDateString(time_set);   
                                           
                os_tsk_delete_self();
                break;
        } 
    }
}
